package com.topica.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.topica.AudioActivity;
import com.topica.ImageSlideActivity;
import com.topica.R;
import com.topica.VideoActivity;
import com.topica.object.LectureItem;

public class LectureAdapter extends ArrayAdapter<LectureItem> {
	private LayoutInflater inflater;
	private Context context;

	public LectureAdapter(Context context, int textViewResourceId,
			List<LectureItem> objects) {
		super(context, textViewResourceId, objects);
		this.context = context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		LectureItem item = getItem(position);
		if (null == convertView) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.layout_item_lecture, null);
			holder.imgBook = (ImageView) convertView
					.findViewById(R.id.lectureText);
			holder.imgAudio = (ImageView) convertView
					.findViewById(R.id.lectureAudio);
			holder.imgVideo = (ImageView) convertView
					.findViewById(R.id.lectureVideo);
			holder.titleLecture = (TextView) convertView
					.findViewById(R.id.txtTitleLecture);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			holder.imgAudio.setOnClickListener(null);
			holder.imgBook.setOnClickListener(null);
			holder.imgVideo.setOnClickListener(null);
		}
		if(!item.isHasAudio()){
			holder.imgAudio.setImageResource(R.drawable.ic_audio_disable);
			holder.imgAudio.setOnClickListener(null);
		} else {
			holder.imgAudio.setImageResource(R.drawable.ic_audio);
			holder.imgAudio.setOnClickListener(new OnLectureAudioClickListener(item
					.getPathAudio()));
		}
		if(!item.isHasBook()){
			holder.imgBook.setImageResource(R.drawable.ic_book_disable);
			holder.imgBook.setOnClickListener(null);
		} else {
			holder.imgBook.setImageResource(R.drawable.ic_book);
			holder.imgBook.setOnClickListener(new OnLectureBookClickListener(item
					.getPathBook()));
		}
		if(!item.isHasVideo()){
			holder.imgVideo.setImageResource(R.drawable.ic_video_disable);
			holder.imgVideo.setOnClickListener(null);
		} else {
			holder.imgVideo.setImageResource(R.drawable.ic_video);
			holder.imgVideo.setOnClickListener(new OnLectureVideoClickListener(item
					.getPathVideo()));
		}
//		holder.imgAudio.setOnClickListener(new OnLectureAudioClickListener(item
//				.getPathAudio()));
//		holder.imgBook.setOnClickListener(new OnLectureBookClickListener(item
//				.getPathBook()));
//		holder.imgVideo.setOnClickListener(new OnLectureVideoClickListener(item
//				.getPathVideo()));
		holder.titleLecture.setText(item.getName());
		return convertView;
	}

	private class OnLectureBookClickListener implements OnClickListener {
		private String path;

		public OnLectureBookClickListener(String path) {
			this.path = path;
		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(context, ImageSlideActivity.class);
			intent.putExtra(ImageSlideActivity.PATH, path);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent);
		}
	}

	private class OnLectureAudioClickListener implements OnClickListener {
		private String path;

		public OnLectureAudioClickListener(String path) {
			this.path = path;
		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(context, AudioActivity.class);
			intent.putExtra(AudioActivity.PATH, path);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent);
		}
	}

	private class OnLectureVideoClickListener implements OnClickListener {
		private String path;

		public OnLectureVideoClickListener(String path) {
			this.path = path;
		}

		@Override
		public void onClick(View v) {
			// Intent intent = new Intent(context, VideoActivity.class);
			// intent.putExtra(VideoActivity.PATH,
			// "http://mobi.topica.vn/tinhnv/video/1720/PSD101_BAI1_avi.mp4");
			// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			// context.startActivity(intent);
			System.out.println("path=" + path);
			if (null == path || path.equals("")) {
				Toast.makeText(context, R.string.no_video, Toast.LENGTH_SHORT)
						.show();
			} else {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setDataAndType(Uri.parse(path), "video/*");
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
		}
	}

	private class ViewHolder {
		private TextView titleLecture;
		private ImageView imgBook;
		private ImageView imgAudio;
		private ImageView imgVideo;
	}

}
